package com.leesche.zcsp.node.web.controller.web.user;

import com.alibaba.fastjson.JSON;
import com.leesche.zcsp.node.utils.security.QEncodeUtil;
import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.gernerator.TimeGreaterUilt;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.template.UserTemplate;
import com.leesche.zcsp.node.web.model.*;
import com.leesche.zcsp.node.web.service.SysAuditPersonService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.service.impl.data.writ.AddUserInfoCommand;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import com.leesche.zcsp.node.web.vo.AuditPerson;
import com.leesche.zcsp.node.web.vo.ChildUserInfo;
import com.leesche.zcsp.node.web.vo.Principal;
import com.leesche.zcsp.node.web.vo.UserGroup;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author 陈立勇
 * @version V1.0
 * @Title: AuditPersonController.java
 * @Description: TODO(审核员信息管理控制器)
 * @date 2016年9月22日 上午10:02:27
 */
@Controller
@RequestMapping("/web/auditperson")
public class AuditPersonController {

    private final static Logger logger = Logger
            .getLogger(AuditPersonController.class);

    @Autowired
    private PrivilegeService privilegeService;
    @Autowired
    private SysAuditPersonService sysAuditPersonService;

    private static final String SHOW_VIEW = "/web/auditperson/showView";
    private static final String ADD_VIEW = "/web/auditperson/addView";
    private static final String EDIT_VIEW = "/web/auditperson/editView";

    @RequestMapping(value = "/showinfo", method = RequestMethod.GET)
    public String showView() {
        return SHOW_VIEW;
    }

    @RequestMapping(value = "/addinfo", method = RequestMethod.GET)
    public String addView() {
        return ADD_VIEW;
    }

    @RequestMapping(value = "{personid}/editinfo", method = RequestMethod.GET)
    public String editView(final @PathVariable("personid") Long id, Model model) {

        QuerySysAuditPersonCondition condition = new QuerySysAuditPersonCondition();
        condition.setAuditpersonid(id);
        SysAuditPersonDto dto =  sysAuditPersonService.findByCondition(condition);

        AuditPerson person = new AuditPerson();
        if(dto != null){
            person.setId(dto.getAuditpersonid());
            person.setMobile(dto.getPersonmobile());
            person.setName(dto.getPersonname());
            person.setEmail(dto.getPersonemail());
        }

        model.addAttribute("persong",person);
        return EDIT_VIEW;
    }

    @ResponseBody
    @RequestMapping(value = "/allinfo", method = RequestMethod.GET)
    public Map<String, Object> getAllInfo(final QuerySysAuditPersonCondition condition) {
        PagingResult<SysAuditPersonDto> pagingResult = sysAuditPersonService.findAllByPage(condition);
        Map<String, Object> json = WebResult.buildOf(pagingResult).toMap();
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "{personid}/removeinfo", method = RequestMethod.POST)
    public Map<String, Object> removeInfo(final @PathVariable("personid") Long id) {
        try {
            sysAuditPersonService.deleteByPrimaryKey(id);
            return WebResult.success().toMap();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/addinfo", method = RequestMethod.POST)
    public Map<String, Object> addInfo(SysAuditPerson person) {
        try {
            person.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
            person.setUpdatetime(new Date());
            sysAuditPersonService.insertSelective(person);
            return WebResult.success().toMap();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/editinfo", method = RequestMethod.POST)
    public Map<String, Object> editInfo(SysAuditPerson person) {
        try {
            person.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
            person.setUpdatetime(new Date());
            sysAuditPersonService.updateByPrimaryKeySelective(person);
            return WebResult.success().toMap();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

}
